package com.polaris.live.ui.main.fragment

import android.os.Bundle
import android.view.View
import androidx.annotation.CallSuper
import androidx.viewbinding.ViewBinding
import androidx.viewpager2.widget.ViewPager2
import com.polaris.live.adapter.home.BannerPromptAdapter
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.mvvm.base2.BaseVbFragment
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.util.SystemUtils
import com.polaris.live.resp.event.BannerPromptEvent
import com.polaris.live.utils.route.RouterUtils
import com.zhpan.bannerview.BannerViewPager
import com.zhpan.bannerview.BaseBannerAdapter
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import kotlin.math.abs

/**
 * AbstractBannerPromptFragment
 *
 * @author Created by lucas on 2024/06/26 07:02
 * @since 1.0.0
 */
abstract class AbstractBannerPromptFragment<VM : BaseViewModel, VB : ViewBinding> : BaseVbFragment<VM, VB>() {

    open val promptBannerView: BannerViewPager<Any>? = null

    private val promptAdapter by lazy { BannerPromptAdapter() }

    @CallSuper
    override fun initView(savedInstanceState: Bundle?) {
        EventBus.getDefault().register(this)

        initPrompt()
    }

    private fun initPrompt() {
        promptBannerView?.apply {
            adapter = promptAdapter as BaseBannerAdapter<Any>
            setIndicatorVisibility(View.GONE)
            setInterval(15 * 1000)
            setScrollDuration(650)
            setUserInputEnabled(false)
            setPageTransformer { page, position ->
                if (position <= -1 || position >= 1) {
                    page.alpha = 0f
                } else if (position == 0f) {
                    page.alpha = 1f
                } else {
                    page.alpha = 1 - abs(position)
                }
            }
            setOnPageClickListener { _, position ->
                val data = promptAdapter.getList().getOrNull(position)
                    ?: return@setOnPageClickListener

                val copyContent = data.copyContent
                if (!copyContent.isNullOrBlank()) {
                    SystemUtils.copyToClipboard(requireContext(), copyContent)
                }

                val router = data.link
                if (!router.isNullOrEmpty()) {
                    RouterUtils.routerGoto(router)
                } else {
                    val nativeRouter = data.nativeLink
                    if (!nativeRouter.isNullOrEmpty()) {
                        SystemUtils.launchThirdApp(nativeRouter)
                    }
                }
            }

            visibility = View.GONE
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onPromptChange(event: BannerPromptEvent) {
        promptBannerView?.also { view ->
            val prompts = event.prompts
            if (!prompts.isNullOrEmpty()) {
                view.create(prompts)

                val layout = view.layoutParams
                if (layout.height < 5) {
                    layout.height = 28.autoDp
                    view.layoutParams = layout
                }

                val page2 = view.findViewById<ViewPager2>(com.zhpan.bannerview.R.id.vp_main)
                page2?.orientation = ViewPager2.ORIENTATION_VERTICAL

                view.visibility = View.VISIBLE
            } else {
                val layout = view.layoutParams
                layout.height = 1
                view.layoutParams = layout
                view.visibility = View.INVISIBLE
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()

        EventBus.getDefault().unregister(this)
    }
}